package com.fnzx.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fnzx.dao.CommentDao;
import com.fnzx.entity.CommentEntity;
import com.fnzx.entity.UserEntity;
import com.fnzx.entity.vo.CommentVo;
import com.fnzx.service.CommentService;
import com.fnzx.service.UserService;
import com.fnzx.util.PageUtils;
import com.fnzx.util.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;


@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentDao, CommentEntity> implements CommentService {

    @Resource
    private CommentDao commentDao;
    @Autowired
    UserService userService;
    @Autowired
    CommentService commentService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CommentEntity> page = this.page(
                new Query<CommentEntity>().getPage(params),
                new QueryWrapper<>()
        );
       List<CommentVo>  commentVos=page.getRecords().stream().map(commentEntity -> {
           UserEntity userEntity = userService.getOne(new QueryWrapper<UserEntity>().eq("id", commentEntity.getPushlisherId()));
           return new CommentVo(commentEntity,userEntity);
       }).collect(Collectors.toList());
        IPage<CommentVo> result = new Page<>();
        BeanUtils.copyProperties(page,result);
        result.setRecords(commentVos);
        return new PageUtils(result);
    }

    @Override
    public PageUtils queryPageByDe(Map<String, Object> params) {
        QueryWrapper<CommentEntity> queryWrapper = new QueryWrapper<CommentEntity>();
        queryWrapper.eq("parent_id",0);
        queryWrapper.eq("demand_id",params.get("demandId"));

        IPage<CommentEntity> page = this.page(
                new Query<CommentEntity>().getPage(params),
                queryWrapper
        );
        List<CommentVo>  commentVos=page.getRecords().stream().map(commentEntity -> {
         //   CommentEntity comment = commentService.getOne(new QueryWrapper<CommentEntity>().eq("pushlisher_id", commentEntity.getPushlisherId()));
            UserEntity user = userService.getOne(new QueryWrapper<UserEntity>().eq("id", commentEntity.getPushlisherId()));
            return new CommentVo(commentEntity,user);
        }).collect(Collectors.toList());

        IPage<CommentVo> result = new Page<>();
        BeanUtils.copyProperties(page,result);
        result.setRecords(commentVos);
        return new PageUtils(result);
    }

    @Override
    public PageUtils queryPageByPa(Map<String, Object> params) {
        QueryWrapper<CommentEntity> queryWrapper = new QueryWrapper<CommentEntity>();

            queryWrapper.eq("parent_id",params.get("commentId"));

        IPage<CommentEntity> page = this.page(
                new Query<CommentEntity>().getPage(params),
                queryWrapper
        );
        List<CommentVo>  commentVos=page.getRecords().stream().map(commentEntity -> {
            UserEntity user = userService.getOne(new QueryWrapper<UserEntity>().eq("id", commentEntity.getPushlisherId()));
            return new CommentVo(commentEntity,user);
        }).collect(Collectors.toList());
        IPage<CommentVo> result = new Page<>();
        BeanUtils.copyProperties(page,result);
        result.setRecords(commentVos);
        return new PageUtils(result);
    }

    @Override
    public List<CommentVo> findChild(Long commentId){
        List<CommentEntity> son = commentDao.getSon(commentId);
        List<CommentVo> vo =new ArrayList<>();
        for (CommentEntity comment:son){

            //BeanUtils.copyProperties(comment,commentVo);
            UserEntity user = userService.getOne(new QueryWrapper<UserEntity>().eq("id", comment.getPushlisherId()));
            CommentVo commentVo = new CommentVo(comment,user);
            vo.add(commentVo);
        }
        return vo;
    }

    @Override
    public List<CommentVo> getAll(Long demandId) {
        List<CommentEntity> all = commentDao.getAll(demandId);
        List<CommentVo> vo =new ArrayList<>();
        for (CommentEntity comment:all){

            //BeanUtils.copyProperties(comment,commentVo);
            UserEntity user = userService.getOne(new QueryWrapper<UserEntity>().eq("id", comment.getPushlisherId()));
            CommentVo commentVo = new CommentVo(comment,user);
            vo.add(commentVo);
        }
        return vo;
    }


}